package com.doge.walknovel.utils

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.android.installreferrer.api.ReferrerDetails

object InstallReferUtils {
    private var installReferrer: String? = null
    val referrer get() = installReferrer

    fun check() {
        log("InstallReferUtils check")
        val client = InstallReferrerClient.newBuilder(appContext).build()
        client.startConnection(object : InstallReferrerStateListener {
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                log("InstallReferUtils onInstallReferrerSetupFinished $responseCode")
                when (responseCode) {
                    InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED -> {
                        feedback("InstallReferUtils onInstallReferrerSetupFinished SERVICE_DISCONNECTED")
                    }
                    InstallReferrerClient.InstallReferrerResponse.OK -> {
                        feedback("InstallReferUtils onInstallReferrerSetupFinished OK")
                        try {
                            installReferrer = client.installReferrer.installReferrer
                            log("InstallReferUtils client.installReferrer info ${client.installReferrer.info}")
                        } catch (e: Throwable) {
                            e.printStackTrace()
                            feedback("InstallReferUtils onInstallReferrerSetupFinished OK installReferrer error")
                        }
                    }
                    InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE -> {
                        feedback("InstallReferUtils onInstallReferrerSetupFinished SERVICE_UNAVAILABLE")
                    }
                    InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED -> {
                        feedback("InstallReferUtils onInstallReferrerSetupFinished FEATURE_NOT_SUPPORTED")
                    }
                    InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR -> {
                        feedback("InstallReferUtils onInstallReferrerSetupFinished DEVELOPER_ERROR")
                    }
                    InstallReferrerClient.InstallReferrerResponse.PERMISSION_ERROR -> {
                        feedback("InstallReferUtils onInstallReferrerSetupFinished PERMISSION_ERROR")
                    }
                }
                client.endConnection()
            }

            override fun onInstallReferrerServiceDisconnected() {
                feedback("InstallReferUtils onInstallReferrerServiceDisconnected")
            }
        })
    }

    private fun feedback(message: String) {
        log("InstallReferUtils $message")
        feedback("InstallReferrer", message, "")
    }
}

val ReferrerDetails.info: String get() {
    return "[installVersion:${installVersion}#installReferrer:${installReferrer}#googlePlayInstantParam:${googlePlayInstantParam}#installBeginTimestampSeconds:${installBeginTimestampSeconds}" +
            "#installBeginTimestampServerSeconds:${installBeginTimestampServerSeconds}#referrerClickTimestampSeconds:${referrerClickTimestampSeconds}#referrerClickTimestampServerSeconds:${referrerClickTimestampServerSeconds}]"
}